package com.javajun.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javajun.entity.MainCourse;
import com.javajun.entity.MainGrade;
import com.javajun.entity.Student;
import com.javajun.service.MainCourseService;
import com.javajun.service.MainGradeService;
import com.javajun.service.StudentService;

/**
 * @ClassName: MainGradeController
 * @Description: 主修课程成绩的控制器
 * @author: xlm
 * @date: 2018年10月15日
 */
@Controller
@RequestMapping(value="/admin/maingrade")
public class MainGradeController {

	@Autowired
	private MainGradeService mainGradeService;
	
	@Autowired
	private MainCourseService mainCourseService;
	
	@Autowired
	private StudentService studentService;
	
	/*@RequestMapping(value="/manage")
	public String Manage(ModelMap map, MainGrade entity) {
		// 所有课程
		List<MainCourse> mainCourses = new ArrayList<>();
		mainCourses = mainCourseService.findAll();
		map.put("mainCourses", mainCourses);
		// 所有学生
		List<Student> students = new ArrayList<>();
		students = studentService.findAll();
		map.put("students", students);
		
		// 所有成绩
		List<MainGrade> mainGrades = new ArrayList<>();
		mainGrades = mainGradeService.findAll(mainGradeService.BuildSpec(entity));
		
		mainGrades = mainGradeService.FindAllByOrderByStduentId();
		for (MainGrade mainGrade : mainGrades) {
			System.out.println("姓名："+mainGrade.getStudent().getName() +",课程名："+ mainGrade.getMainCourse().getName()+",成绩："+mainGrade.getGrade());
		}
		map.put("mainGrades", mainGrades);
		return "maingrade/manage";
	}*/
	
	@RequestMapping(value="manage")
	public String Manage(ModelMap map) {
		List<MainGrade> models = mainGradeService.FindAllByOrderByStduentId();
		map.put("models", models);
		return "maingrade/manage";
	}
	
	// 修改页面
	@RequestMapping(value="/edit")
	public String Edit(ModelMap map, Integer id) {
		MainGrade model = new MainGrade();
		if (id != null) {
			model = mainGradeService.findById(id);
		}
		map.put("model", model);
		return "maingrade/edit";
	}
	
	// 保存
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String Save(ModelMap map, MainGrade entity) {
		MainGrade mainGrade = new MainGrade();
		if (entity.getId() != null) {
			mainGrade = mainGradeService.findById(entity.getId());
		}
		if (entity.getStudent().getId() != null) {
			Student student = studentService.findById(entity.getStudent().getId());
			mainGrade.setStudent(student);
		}
		if (entity.getMainCourse().getId() != null) {
			MainCourse mainCourse = new MainCourse();
			mainCourse = mainCourseService.findById(entity.getMainCourse().getId());
			mainGrade.setMainCourse(mainCourse);
		}
		if (entity.getGrade() != null) {
			mainGrade.setGrade(entity.getGrade());
		}
		mainGradeService.save(mainGrade);
		return "redirect:manage";
	}
	
	// 删除
	@RequestMapping(value="delete")
	public String Delete(ModelMap map, Integer id) {
		MainGrade mainGrade = new MainGrade();
		if (id != null) {
			mainGrade = mainGradeService.findById(id);
			mainGradeService.delete(mainGrade);
		}
		return "redirect:manage";
	}

	// 新增页面
	@RequestMapping(value="add")
	public String Add(ModelMap map) {
		List<Student> students = new ArrayList<>();
		students = studentService.findAll();
		map.put("students", students);
		List<MainCourse> mainCourses = mainCourseService.findAll();
		map.put("mainCourses", mainCourses);
		return "maingrade/add";
	}
}
